package com.ec.vs.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ec.vs.entity.VsAppointment;
import com.ec.vs.vo.VsAppointmentVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;

@Mapper
public interface VsMapper extends BaseMapper<VsAppointment> {


    // 根据 ID 更新整个预约记录（用于修改/审核）
    // 使用 MyBatis-Plus 自带的 updateById 即可，无需额外声明

    // 取消预约：仅更新状态为“已取消”
    @Update("UPDATE vs_appointment SET status = 2 WHERE id = #{id}")
    int updateStatusToCanceled(@Param("id") Long id);

    @Select("SELECT COUNT(*) FROM vs_appointment WHERE time = #{time} AND status IN (0, 1)")
    int countTimeConflict(VsAppointmentVo appointmentVo);

    // 统计给定日期内的预约总数
    @Select("SELECT COUNT(*) FROM vs_appointment WHERE DATE(time) = DATE(#{time}) AND status IN (0, 1)")
    int countAppointmentsByDate(@Param("time") LocalDateTime time);

    // 检查用户在一天内的提交总数
    @Select("SELECT COUNT(*) FROM vs_appointment WHERE user_id = #{userId} AND DATE(time) = DATE(#{time}) AND status IN (0, 1)")
    int countUserAppointmentsByDate(@Param("userId") Long userId, @Param("time") LocalDateTime time);

    // 检查用户是否已经在同一时间提交了相同的预约
    @Select("SELECT COUNT(*) FROM vs_appointment WHERE user_id = #{userId} AND time = #{time} AND status IN (0, 1)")
    int countUserAppointmentsByTime(@Param("userId") Long userId, @Param("time") LocalDateTime time);

    @Update("UPDATE vs_appointment SET status = 1 WHERE id = #{id} AND status = 0")
    int updateStatusToApproved(@Param("id") Long id);
}